
import struct

def create_new_font(add_file, old_total_file, out_ota_file, out_all_file):
    old_total = open(old_total_file, "rb")
    add = open(add_file, "rb")
    old_data = old_total.read()
    add_data = add.read()
    old_size = len(old_data)
    old_version = old_data[0:2].decode("ASCII")
    new_version = "%02d"%((int(old_version[0])*10)+(int(old_version[1]))+1)
    new_version_ASCII = new_version.encode("ASCII")
    new_total = open(out_all_file, "wb")
    new_total.write(new_version_ASCII)
    new_total.write(old_data[2:])
    pad = None
    if old_size % (128*1024):
        pad = 128*1024 - old_size%(128*1024)
    else:
        pad = 0
    pad_data = bytearray([0xff]*pad)
    new_total.write(pad_data)
    ota_start_addr = old_size+pad
    print("ota addr 0x%08x"%ota_start_addr)
    new_total.write(add_data)
    
    out_ota = open(out_ota_file, "wb")
    out_ota.write(struct.pack("<I", ota_start_addr))
    out_ota.write(add_data)
    out_ota.write((new_version+"OK").encode("ASCII"))
    
    add.close()
    out_ota.close()
    new_total.close()
    old_total.close()
    

if __name__ == "__main__":
    create_new_font("AllBinData.bin", "L42AP_FONT_F04_Build10.bin", "Picture_11.bin", "L42AP_FONT_F04_Build11.bin")
    